<template>
    <div class="container">
      <!-- 导航固定定位 fixed -->
      <van-nav-bar
        fixed
        title="搜索结果"
        left-arrow
        @click-left="$router.back()"
      />
      <!-- 文章列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad()"
      >
        <van-cell-group>
          <van-cell :to="`/article/${item.art_id}`" v-for="(item,index) in list" :key="index">
            <div class="article_item">
              <h3 class="van-ellipsis">
                {{item.title}}
              </h3>
              <div class="img_box" v-if="item.cover.images.length">
                <van-image
                  v-for="img in item.cover.images" :key="img"
                  :class="item.cover.type === 3? 'w33': 'w100'"
                  fit="cover"
                  :src="img"
                />
              </div>
              <div class="info_box">
              <span>{{item.aut_name}}</span>
              <span>{{item.comm_count}}评论</span>
              <span>{{item.pubdate|relTime}}</span>
              </div>
            </div>
          </van-cell>
        </van-cell-group>
      </van-list>
    </div>
</template>

<script>
// 导入文章列表的数据
import { searchArticles } from '@/api/article'
export default {
  name: 'search-result',
  data () {
    return {
      loading: false, // 加载状态 true代表加载中  false代表加载状态结束
      finished: false, // 数据全部加载结束
      list: [] // 列表数据
    }
  },
  methods: {
    async onLoad () {
      // 发送接口请求
      const { status, data } = await searchArticles(this.query)
      // console.log(status, data)
      if (status === 200) {
        // 接口发送成功之后将获取到的数据追加到list中
        this.list.push(...data.results)
        // 加载状态改为false
        this.loading = false
        // 判断数据是否加载完
        if (this.list.length === data.total_count) {
          this.finished = true
        } else {
          this.query.page++
        }
      }
    }
  },
  created () {
    // 初始化分页数据
    this.query = {
      page: 1,
      q: this.$route.query.q
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding-top: 46px;
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      height: 180px;
      width: 100%;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    span {
      padding-right: 10px;
    }
  }
}
</style>
